<script setup>
import { h, onMounted, ref, resolveDirective, withDirectives } from 'vue'
import {
  NButton,
  NCheckbox,
  NCheckboxGroup,
  NForm,
  NFormItem,
  NImage,
  NInput,
  NSpace,
  NSwitch,
  NTag,
  NPopconfirm,
  NLayout,
  NLayoutSider,
  NLayoutContent,
  NTreeSelect, NInputNumber,
} from 'naive-ui'

import CommonPage from '@/components/page/CommonPage.vue'
import QueryBarItem from '@/components/query-bar/QueryBarItem.vue'
import CrudModal from '@/components/table/CrudModal.vue'
import CrudTable from '@/components/table/CrudTable.vue'

import { formatDate, renderIcon } from '@/utils'
import { useCRUD } from '@/composables'
// import { loginTypeMap, loginTypeOptions } from '@/constant/data'
import api from '@/api'
import TheIcon from '@/components/icon/TheIcon.vue'
import { useUserStore } from '@/store'

defineOptions({ name: '底薪设置' })

const $table = ref(null)
const queryItems = ref({})
const vPermission = resolveDirective('permission')

const {
  modalVisible,
  modalTitle,
  modalAction,
  modalLoading,
  handleSave,
  modalForm,
  modalFormRef,
  handleEdit,
  handleDelete,
  handleAdd,
} = useCRUD({
  name: '底薪设置',
  initForm: {},
  doCreate: api.createUser,
  doUpdate: api.updateUser,
  doDelete: api.deleteUser,
  refresh: () => $table.value?.handlerefresh(),
})

const roleOption = ref([])
const deptOption = ref([])
const timestamp = ref(getCurrentMonthTimestamp());
function getCurrentMonthTimestamp() {
  const now = new Date();
  const year = now.getFullYear();
  const month = now.getMonth();
  const firstDayOfMonth = new Date(year, month, 1);
  return firstDayOfMonth.getTime();
}


function formatTimestamp(timestamp) {
  const date = new Date(timestamp)
  const pad = (num) => num.toString().padStart(2, '0')
  const year = date.getFullYear()
  const month = pad(date.getMonth() + 1) // 月份从0开始，所以需要+1
  const day = pad(date.getDate())
  const hours = pad(date.getHours())
  const minutes = pad(date.getMinutes())
  const seconds = pad(date.getSeconds())
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

const handleDateRangeChange = (value) => {
  queryItems.value.start_time = formatTimestamp(value)
}

onMounted(() => {
  queryItems.value.yeji = 1
  $table.value?.handleSearch()
})

const columns = [
  {
    title: 'ID',
    key: 'id',
    width: 30,
    align: 'center',
    ellipsis: {tooltip: true},
  },
  {
    title: '员工姓名',
    key: 'alias',
    width: 60,
    align: 'center',
    ellipsis: {tooltip: true},
    fixed: 'left',
  },
  {
    title: '员工类型',
    key: 'role',
    width: 60,
    align: 'center',
    render(row) {
      const roles = row.roles ?? []
      const group = []
      for (let i = 0; i < roles.length; i++)
        group.push(
            h(NTag, {
              type: 'primary',
              size: "small",
              bordered: false,
              style: {margin: '2px 3px'}
            }, {default: () => roles[i].name})
        )
      return h('span', group)
    },
  },
  {
    title: '部门',
    key: 'dept.name',
    align: 'center',
    width: 40,
    ellipsis: {tooltip: true},
  }, {
    title: '底薪',
    key: 'salary',
    align: 'center',
    width: 40,
    ellipsis: {tooltip: true},
  }, {
    title: '提成',
    key: 'tc',
    align: 'center',
    width: 40,
    ellipsis: {tooltip: true},
  }, {
    title: '合计应发',
    key: 'hj',
    align: 'center',
    width: 40,
    ellipsis: {tooltip: true},
  },

]

</script>

<template>

      <CommonPage show-footer title="核对发放" style="padding: 0;margin: 0;">
        <!-- 表格 -->
        <CrudTable
            ref="$table"
            v-model:query-items="queryItems"
            :columns="columns"
            :get-data="api.getUserList"
            :scroll-x="1200"
        >
          <template #queryBar>
            <QueryBarItem label="统计月份" :label-width="80">
              <n-date-picker
                v-model:value="timestamp"
                type="month"
                format="y年 M月"
                year-format="y年"
                month-format="M月"
                clearable
                @update:value="handleDateRangeChange"
              />
            </QueryBarItem>

          </template>
        </CrudTable>

      </CommonPage>

  <!-- 业务页面 -->
</template>
